package data.structures.stack;


public class LinkListStackDemo {
    public static void main(String[] args) {
        LinkListStack linkListStack = new LinkListStack(10);
        linkListStack.push(10);
        linkListStack.push(20);
        linkListStack.push(30);


        linkListStack.list(linkListStack.first);
    }

}

class LinkListStack {
    private int maxSize;
    public LinkNodeStack first;
    private int top;


    public LinkListStack(int maxSize) {
        this.maxSize = maxSize;
        first = new LinkNodeStack(0);
        this.top = -1;
    }

    public boolean isEmpty() {
        return top == -1;
    }
    //栈满
    public boolean isFull() {
        return top == maxSize - 1;
    }

    public void push(int value){
        //先判断栈是否满
        if(isFull()) {
            System.out.println("栈满");
            return;
        }
        LinkNodeStack temp = first;
        while (true){
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        top++;
        temp.next = new LinkNodeStack(value);
    }
    public void list(LinkNodeStack linkNodeStack){
        //先判断栈是否满
        if(isEmpty()) {
            System.out.println("栈空，没有数据~");
            return;
        }
        //辅助节点
        LinkNodeStack cur = linkNodeStack.next;
        LinkNodeStack next = null;
        LinkNodeStack reverseStack = new LinkNodeStack(0);
        while (cur != null) {
            next = cur.next;
            cur.next = reverseStack.next;
            reverseStack.next = cur;
            cur = next;
        }
        LinkNodeStack temp = reverseStack.next;
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }


}


class LinkNodeStack {
    public int no;
    public LinkNodeStack next; //指向下一个节点

    public LinkNodeStack(int no) {
        this.no = no;
    }

    @Override
    public String toString() {
        return "LinkNodeStack{" +
                "no=" + no +
                '}';
    }
}

